<template>
  <ShowContentVue
      :textContent="TextContent"
      @btnEvent="login"
      @textEvent="toRegister"
      :userFormData="userFormData"
  />
</template>

<script setup>
import ShowContentVue from '@/components/ShowContent.vue';
import {
  reactive
} from 'vue'
import {
  useLoginStore,
  useTagLibraryStore,
  useUserStore
} from '../../store'

const TextContent = reactive({
  btnText: '登录',
  text: '去注册',
  welcome: '您好,欢迎登录!',
  placeHolderText: {
    fText: '账号',
    sText: '密码',
    isShowCode: false
  },
  placeHolderStyle: {
    color: '#FB7685',
    paddingRight: '48rpx'
  },
})

const loginStore = useLoginStore()
const tagLibraryStore = useTagLibraryStore()
const userStore = useUserStore()
const userFormData = reactive({
  phone: '',
  password: ''
})

// 登录用户是否选择过标签
function findUserIsCheckTagLabel(temp1) {
  for (let key in temp1) {
    for (let item of temp1[key]) {
      if (item.tagLibraryBO.userSelected) {
        return true
      }
    }
  }
}

// 登录
const login = async ({checkedAgree, modelShow, content}) => {
  if (userFormData.password === "" || userFormData.phone === "") {
    uni.showToast({
      title: '用户名密码为空'
    })
    return
  }
  let res = await loginStore.loginUser({
    ...userFormData
  })
  uni.showLoading({
    title: "登录中..."
  })
  if (res?.code == 0) {
    uni.setStorageSync('userInfo', {
      userId: res.data.id,
      token: res.data.jwt,
      nickname: res.data.nickname
    })
    await userStore.getUserDetail()
    const result = await tagLibraryStore.userList()
    uni.hideLoading()
    // const isChecked = findUserIsCheckTagLabel(result)
    if (userStore.userInfo.userInfos.status) {
      uni.switchTab({
        url: '/pages/tab-index/index'
      })
    } else {
      uni.navigateTo({
        url: '/pages/tab-mine/drawer/identity-tag'
      })
    }

  } else {
    content.value = "用户名密码错误"
    modelShow.value = true
    userFormData.password = ""
    userFormData.phone = ""
  }
}

//注册
const toRegister = () => {
  uni.navigateTo({
    url: "/pages/register/index"
  })
}
</script>

<style scoped lang="scss">
::v-deep .u-input__content__field-wrapper__field {
  padding: 10rpx 0 10rpx 30rpx;
  color: #CCCCCC;
}
</style>